package com.rich.user.service.impl;/**
 * @Package: com.rich.user.service.impl
 * @Author: Lin
 * @Date: Created in 2021/8/25 9:44
 */

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.rich.common.constant.SuggestEnum;
import com.rich.common.entity.TbSuggest;
import com.rich.common.result.CommonResponse;
import com.rich.common.result.ResultData;
import com.rich.common.util.SendSms;
import com.rich.user.mapper.TbSuggestMapper;
import com.rich.user.service.SuggestService;
import com.rich.user.vo.SuggestVo;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @program: richhouse
 *
 * @author: Mr.Lin
 *
 * @create: 2021-08-25 09:44
 *
 **/
@Service
public class SuggestServiceImpl implements SuggestService {
    @Autowired
    private TbSuggestMapper suggestMapper;
    @Autowired
    private RabbitTemplate rabbitTemplate;
    /**
     * 发送问题
     *
     * @param suggest
     */
    @Override
    public CommonResponse sendSuggest(TbSuggest suggest) {
        //判断当前用户是否已经用完当日的问题反馈次数  每日5次
        Integer i = suggestMapper.findSuggestCountByUserId(suggest.getUserId());
        if (i > SuggestEnum.Suggest_FIVE) {
            return new CommonResponse(SuggestEnum.SEND_LOSE_CODE,SuggestEnum.SEND_LOSE_MESSAGE);
        }
        //小于5次
        suggestMapper.sendSuggeat(suggest);
        return new CommonResponse(SuggestEnum.SEND_SUCCESS_CODE,SuggestEnum.SEND_SUCCESS_MESSAGE);
    }

    /**
     * 反馈问题列表
     * @param suggestVo
     * @return
     */
    @Override
    public ResultData findSuggestAll(SuggestVo suggestVo) {
        PageHelper.startPage(suggestVo.getPage(),suggestVo.getLimit());
        List<TbSuggest> suggestList = suggestMapper.findSuggestAll(suggestVo);
        PageInfo<TbSuggest> map = new PageInfo<>(suggestList);
        return new ResultData(map.getList(),map.getTotal());
    }

    /**
     * 修改问题解决状态
     * @param id
     * @param status
     */
    @Override
    public void updSuggestStatusById(Integer id,Integer status) {
        //先根据id进行修改
        suggestMapper.updSuggestStatusById(id,status);
        //根据id获取单条
        TbSuggest suggest = suggestMapper.findSuggestById(id);


            //rabbit 异步监听发送短信
            HashMap<String, String> map = new HashMap<>(2);
//            map.put("phone",suggest.getPhone());
//            map.put("code","1213");
//            rabbitTemplate.convertAndSend("coco",map);

            /**
             * 模拟
             */
        map.put("phone", suggest.getPhone());
        map.put("msg","用户您好！您在RichHouse平台提交的反馈问题已解决，快来看看吧！感谢您的反馈");
        rabbitTemplate.convertAndSend("suggest",map);


    }

}
